package org.example.leetcode;


import java.util.Arrays;

/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/3 18:49
 * @version: 1.0
 */
public class LC41 {
    public static void main(String[] args) {
        LC41 lc=new LC41();
        int[] nums={0,2,2,1,1};
        System.out.println(lc.firstMissingPositive(nums));
    }

    public int firstMissingPositive(int[] nums) {
        Arrays.sort(nums);
        int cutoff=0;
        for (int i = 0; i <nums.length ; i++) {
            if (nums[i]>=1){
                cutoff=i;
                break;
            }
        }
        int[] newNums=new int[nums.length-1-cutoff+1];
        for (int i = 0; i <nums.length-cutoff ; i++) {
            newNums[i]=nums[i+cutoff];
        }
        return helper(newNums);
    }

    private int helper(int[] nums){
        if (nums[0]!=1){
            return 1;
        } else {
            //全是整数的情况
            for (int i = 1; i <nums.length; i++) {
                if (nums[i]==nums[i-1]) continue;
                if (nums[i]-nums[i-1]==1){
                    continue;
                }else {
                    return nums[i-1]+1;
                }
            }
            return nums[nums.length-1]+1;
        }
    }

}
